package ru.usu.gv.gui.behavior;

import ru.usu.gv.gui.analysis.IGraphAnalyzer;

import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.WakeupOnElapsedFrames;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 @author Sergey Pupyrev 
 12.04.2007
 */

public class GraphAnalysBehavior extends Behavior
{
	private WakeupOnElapsedFrames w = new WakeupOnElapsedFrames(100);
	private List<IGraphAnalyzer> graphAnalyzers = new ArrayList<IGraphAnalyzer>();

	public GraphAnalysBehavior(BoundingSphere bound)
	{
		setSchedulingBounds(bound);
	}

	@Override
	public void initialize()
	{
		wakeupOn(w);
	}

	@Override
	public void processStimulus(Enumeration arg0)
	{
		wakeupOn(w);
		for (IGraphAnalyzer item : graphAnalyzers)
		{
			System.out.println("Executing analyzer of class '" + item.getClass().getName() + "'");
			item.doAnalyse();
		}
		setEnable(false);
		needRecalculate = false;
	}

	public void addAnalyzer(IGraphAnalyzer analyzer)
	{
		graphAnalyzers.add(analyzer);
	}

	private boolean needRecalculate = true;
	private boolean visible = false;

	public void setNeedRecalculate(boolean recalculate)
	{
		needRecalculate = recalculate;
	}

	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}

	public void setEnabledInternal()
	{
		if (visible && needRecalculate)
		{
			setEnable(true);
		}
	}
}
